//
//  YHTransactionLookDetailsViewController.m
//  YHApp
//
//  Created by -- on 2021/7/7.
//

#import "HATransactionLookDVC.h"
#import "HATransactionLookDMo.h"

@interface HATransactionLookDVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HATransactionLookDMo *model;

@end

@implementation HATransactionLookDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"交易详情";
    
    [self initUI];
    [self loadData];

    // Do any additional setup after loading the view.
}

- (void)initUI{

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    
    if ([title isEqualToString:@"头部"]) {
        return 250;
    }else {
        return 54;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeNolCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMeNolCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"头部"]) {
        [self setCellHeadContentViewWith:cell.contentView];
    }else if ([title isEqualToString:@"交易编号"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.order_sn isLine:YES];
    }else if ([title isEqualToString:@"商户名称"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.customer_short_name isLine:YES];
    }else if ([title isEqualToString:@"商户编号"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.customer_number isLine:YES];
    }else if ([title isEqualToString:@"交易卡号"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:@"" isLine:YES];
    }else if ([title isEqualToString:@"交易渠道"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.canal_name isLine:YES];
    }else if ([title isEqualToString:@"机具编码"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.termSn isLine:YES];
    }else if ([title isEqualToString:@"交易时间"]) {
        [self setCellTextContentViewWith:cell.contentView title:title content:self.model.created_at isLine:YES];
    }
    return cell;

    return [UITableViewCell new];
}



-(void)loadData {
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"trade/detail.html";
    request.parms = @{@"trade_id":self.trade_id?:@""};

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HATransactionLookDMo mj_objectWithKeyValues:dataModel.data];
                
                [self.dataArray removeAllObjects];
                
                [self.dataArray addObject:@"头部"];
                [self.dataArray addObject:@"交易编号"];
                [self.dataArray addObject:@"商户名称"];
                [self.dataArray addObject:@"商户编号"];
                [self.dataArray addObject:@"交易卡号"];
                [self.dataArray addObject:@"交易渠道"];
                [self.dataArray addObject:@"机具编码"];
                [self.dataArray addObject:@"交易时间"];
                
                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)setCellHeadContentViewWith:(UIView *)contentView  {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UILabel *moneyLabel = [UILabel ha_BLabelWithFont:34 textColor:@"#000000" text:[NSString stringWithFormat:@"￥%@",self.model.amount] alignment:1];
    [contentView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-25);
    }];
    
    UILabel *exLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#0EC578" text:@"" alignment:1];
    [contentView addSubview:exLabel];
    [exLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(moneyLabel.mas_top).offset(-35);
    }];
    
    UIImageView *iconImgView = [UIImageView ha_createImageView];
    [contentView addSubview:iconImgView];
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(exLabel.mas_top).offset(-20);
        make.width.mas_offset(40);
        make.height.mas_offset(40);
    }];
    
    if (self.model.status == 1) {
        exLabel.text = @"交易成功";
        iconImgView.image = [UIImage imageNamed:@"ha_39"];
    }else {
        exLabel.text = @"交易失败";
        iconImgView.image = [UIImage imageNamed:@"ha_40"];
    }
}


-(void)setCellTextContentViewWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *label = [[UILabel alloc]init];
    label.text = title;
    label.textColor = [UIColor colorWithHexString:@"#111a34"];
    label.font = [UIFont systemFontOfSize:16];
    [contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.left.equalTo(contentView).offset(15);
        make.width.equalTo(@80);
    }];
    
    UILabel *contentLabel = [[UILabel alloc]init];
    contentLabel.text = content;
    contentLabel.textColor = [UIColor colorWithHexString:@"#111a34"];
    contentLabel.font = [UIFont boldSystemFontOfSize:16];
    contentLabel.textAlignment = NSTextAlignmentRight;
    [contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.left.equalTo(label).offset(15);
        make.right.equalTo(contentView.mas_right).offset(-15);
    }];
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView).offset(-15);
            make.height.equalTo(@1);
            make.bottom.equalTo(contentView);
        }];
    }
}



- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}



@end
